<?php

namespace app\controllers;

use Yii;
use app\models\CurrentToilet;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\db\Query;
use yii\db\Expression;

/**
 * CurrentToiletController implements the CRUD actions for CurrentToilet model.
 */
class CurrentToiletController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all CurrentToilet models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => CurrentToilet::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single CurrentToilet model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new CurrentToilet model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CurrentToilet();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing CurrentToilet model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing CurrentToilet model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the CurrentToilet model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CurrentToilet the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CurrentToilet::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**
     * Lists all ToliteHistory models.
     * @return mixed
     */
    public function actionCharts($id=0, $co='')
    {
        $flow = (new Query())->from('flow')->where(['t_id' => $id])
        ->orderBy(['id' => SORT_DESC])->limit(60)->all();
        $gas = (new Query())->from('gas')->where(['t_id' => $id])
        ->orderBy(['id' => SORT_DESC])->limit(60)->all();
        $use_histroy = (new Query())->from('use_history')->where(['t_id' => $id])
        ->orderBy(['id' => SORT_DESC])->limit(60)->all();
        $current = (new Query())->from('current_toilet')->where(['t_id' => $id])->all();
        $toilet = (new Query())->from('toilet')->where(['t_id' => $id])->all();
        $fh = (new Query())->from('flow')->where(['t_id' => $id])->
        andWhere(['>','time',new Expression('CURDATE()')])->groupBy(
        new Expression("DATE_FORMAT(`time`, '%Y-%m-%d %H')"))->limit(60)->all();
        $gh = (new Query())->from('gas')->where(['t_id' => $id])->
        andWhere(['>','time',new Expression('CURDATE()')])->groupBy(
        new Expression("DATE_FORMAT(`time`, '%Y-%m-%d %H')"))->limit(60)->all();
        return $this->render('charts', [
            'current'=>$current,
            'gas'=>$gas,
            'flow'=>$flow,
            'use_history'=>$use_histroy,
            'toilet'=>$toilet,
            'flow_hour'=>$fh,
            'gas_hour'=>$gh,
        ]);
    }

    public function actionFlow($id=0, $type=0)
    {
        $flow = (new Query())->from('flow')->where(['t_id' => $id])
        ->orderBy(['id' => SORT_DESC])->limit(60)->all();
        $days = (new Query())->from('flow_day')->where(['t_id' => $id])
        ->orderBy(['id' => SORT_DESC])->limit(60)->all();
        $month = (new Query())->from('flow_mon')->where(['t_id' => $id])->all();
        $year = (new Query())->from('flow_yr')->where(['t_id' => $id])->all();
        $toilet = (new Query())->from('toilet')->where(['t_id' => $id])->all();

        return $this->render('flow', [
            'flow'=>$flow,
            'days'=>$days,
            'month'=>$month,
            'year'=>$year,
            'toilet'=>$toilet
        ]);
    }
}
